package com.pinyougou.shop.controller;

import com.lening.pojo.TbSeller;
import com.lening.sellergoods.service.SellerService;
import entity.PageResult;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/seller")
public class SellerController {

	@Resource
	private SellerService sellerService;

	@RequestMapping("/ceshi")
	public void ceshi(){
		System.out.println(111);
	}

	@RequestMapping("findSellerById")
	public Result findSellerById(String sellerId){
		if(sellerId!=null&&!"".equals(sellerId)){
			Integer count = sellerService.findSellerById(sellerId);
			System.out.println("验证用户唯一------- "+count);
			if (count>0) {
				return  new Result(false,"用户名已存在");
			}else{
				return  new Result(true,"用户名被允许");
			}
		}
		return null;
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbSeller> findAll(){
		return sellerService.findAll();
	}
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows){
		return sellerService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param seller
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody TbSeller seller){

		//密码加密，在这里加密，不能在service中去加入依赖  BCrypt算法加密

		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		System.out.println("加密前"+seller.getPassword());
		String password = passwordEncoder.encode(seller.getPassword());
		seller.setPassword(password);
		System.out.println("加密后"+password);
		try {
			sellerService.add(seller);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param seller
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody TbSeller seller){
		try {
			String userName = getSellerId();
			seller.setSellerId(userName);
			sellerService.update(seller);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbSeller findOne(){
		String userName = getSellerId();
		return sellerService.sellerfindOne(userName);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(String [] ids){
		try {
			sellerService.delete(ids);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbSeller seller, int page, int rows  ){
		return sellerService.findPage(seller, page, rows);		
	}



	//商家修改密码
	@RequestMapping("upaPassword")
	public Result upaPassword(@RequestBody TbSeller seller, String newPassword){
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		String userName = getSellerId();
		TbSeller tbSeller = sellerService.findOne(userName); //数据库的密码

		/**
		 *  matches方法第一个参数是未加密的密码,第二个参数是加密后的密码,比较之后得到一个boolean类型的值
		 *  encoder提供一个未加密与加密密码比较的方法,返回值是boolean类型
		 */
		boolean matches = encoder.matches(seller.getPassword(), tbSeller.getPassword());
		if(matches){
		    	seller.setSellerId(userName);
		    	seller.setPassword(encoder.encode(newPassword));
                sellerService.update(seller);
			return new Result(true,"修改成功");
		}else{
			return new Result(false,"原密码不匹配");
		}
	}


	public String getSellerId(){
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		return name;
	}


    //管理员强制修改密码
	@RequestMapping("updatePassword")
	public String updatePassword(String newPassword,String userName){
		System.out.println(newPassword);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		TbSeller tbSeller = new TbSeller();
		tbSeller.setPassword(encoder.encode(newPassword)); //处理后的密码加进去
		tbSeller.setSellerId(userName);
		sellerService.update(tbSeller);
		return "修改成功";
	}
}
